<?php
App::uses('AppController', 'Controller');
/**
 * Events Controller
 *
 * @property Event $Event
 */
class EventsController extends AppController {

 public $helpers = array('Html', 'Form');
/**
 * index method
 *
 * @return void
 */
	public function index() {
		$this->Event->recursive = 0;
		$this->set('events', $this->paginate());
	}
	
	public function all_events_js() {
		$this->layout = "cleanjs";
		$this->Event->recursive = 0;		
		$this->set('Events',$this->Event->find('all', array('limit' => 500)));
	}

/**
 * view method
 *
 * @param string $id
 * @return void
 */
	public function view($id = null) {
		$this->Event->id = $id;
		if (!$this->Event->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		$this->set('event', $this->Event->read(null, $id));
	}
	
	public function view_public_event($id = null) {
		$this->Event->id = $id;
		if (!$this->Event->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		$this->set('event', $this->Event->read(null, $id));
	}

/**
 * add method
 *
 * @return void
 */
	public function add() {
		if ($this->request->is('post')) {
			$this->Event->create();
			if ($this->Event->save($this->request->data)) {
				$this->Session->setFlash(__('El evento ha sido guardado'));
				$this->redirect(array('action' => 'index'));
			} else {
				$this->Session->setFlash(__('No se pudo guardar, intente nuevamente'));
			}
		}
		$ecategories = $this->Event->Ecategory->generateTreeList(null,null,null,'-',null);
		$places = $this->Event->Place->find('list');
		$users = $this->Event->User->find('list');
		$this->set(compact('ecategories', 'places', 'users'));
	}


	public function add_event_pop($place_id) {
		$this->layout = "clean";
		$vector = $this->request->data;
		if ($this->request->is('post')) {
			$this->Event->create();			
			if ( $vector['Event']['user_id'] == $this->UserAuth->getUserId() &&  $this->Event->save($this->request->data)) {
				$this->Session->setFlash(__('El evento ha sido guardado'));
					$this->redirect(array('controller'=>'events','action' => 'view_public_event',$this->Event->getInsertID()));
			} else {
				$this->Session->setFlash(__('No se pudo guardar, intente nuevamente'));
			}
		}
		$ecategories = $this->Event->Ecategory->generateTreeList(null,null,null,'-',null);
		$places = $this->Event->Place->find('list');
		$users = $this->Event->User->find('list');
		$place_default=$place_id;
		$this->set(compact('ecategories', 'places', 'users'));
		$this->set(compact('place_default'));
		$this->set('user_id',$this->UserAuth->getUserId());
		$this->set('place_id',$place_id);
	}

/**
 * edit method
 *
 * @param string $id
 * @return void
 */
	public function edit($id = null) {
		$this->Event->id = $id;
		if (!$this->Event->exists()) {
			throw new NotFoundException(__('Invalid event'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Event->save($this->request->data)) {
				$this->Session->setFlash(__('El evento ha sido guardado'));
							$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('No se pudo guardar, intente nuevamente'));
			}
		} else {
			$this->request->data = $this->Event->read(null, $id);
		}
		$ecategories = $this->Event->Ecategory->find('list');
		$places = $this->Event->Place->find('list');
		$users = $this->Event->User->find('list');
		$this->set(compact('ecategories', 'places', 'users'));
	}
	
	public function edit_public($id = null) {
		$this->layout = "clean";
		$this->Event->id = $id;
		if (!$this->Event->exists()) {
			throw new NotFoundException(__('Invalid event'));
		}
		if ($this->request->is('post') || $this->request->is('put')) {
			if ($this->Event->save($this->request->data)) {
				$this->Session->setFlash(__('El evento ha sido guardado'));
							$this->redirect($this->referer());
			} else {
				$this->Session->setFlash(__('No se pudo guardar, intente nuevamente'));
			}
		} else {
			$this->request->data = $this->Event->read(null, $id);
		}
		$ecategories = $this->Event->Ecategory->find('list');
		$places = $this->Event->Place->find('list');
		$users = $this->Event->User->find('list');
		$this->set(compact('ecategories', 'places', 'users'));
	}

/**
 * delete method
 *
 * @param string $id
 * @return void
 */
	public function delete($id = null) {
		if (!$this->request->is('post')) {
			throw new MethodNotAllowedException();
		}
		$this->Event->id = $id;
		if (!$this->Event->exists()) {
			throw new NotFoundException(__('Entrada invalida'));
		}
		if ($this->Event->delete()) {
			$this->Session->setFlash(__('Evento borrado'));
			//$this->redirect(array('action' => 'index'));
			$this->redirect($this->referer());
		}
		$this->Session->setFlash(__('No se puede borrar'));
		$this->redirect(array('action' => 'index'));
	}
}
